{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bd4a3b5b",
   "metadata": {},
   "source": [
    "关联规则要装以下pip的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3870cfa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install mlxtend   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0e9a0441",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import pandas as pd\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "from mlxtend.frequent_patterns import apriori,fpgrowth,fpmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4f55329f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#这份数据集无法做成dataframe类型的列表，因为有些列可能会有很多缺失值\n",
    "dataset =[['牛奶','洋葱','香料','芸豆','鸡蛋','酸奶'],\n",
    "['菠萝','洋葱','香料','芸豆','鸡蛋','酸奶'],\n",
    "['牛奶','黄瓜','玉米','芸豆','酸奶'],\n",
    "['玉米','洋葱','芸豆','冰淇淋','鸡蛋']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4cab351",
   "metadata": {},
   "source": [
    "1.数据处理"
   ]
  },
  {
   "cell_type": "raw",
   "id": "bc323e0c",
   "metadata": {},
   "source": [
    "注意：关联规则加载上来的csv数据是没有列的，那怎么搞？具体的方式看视频'关联规则实操',下面也是操作方式"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2b3ca119",
   "metadata": {},
   "source": [
    "1.将数据处理为boolean类型的列表，也就是dataframe类型,也就是转成bool矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "de6e83d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>冰淇淋</th>\n",
       "      <th>洋葱</th>\n",
       "      <th>牛奶</th>\n",
       "      <th>玉米</th>\n",
       "      <th>芸豆</th>\n",
       "      <th>菠萝</th>\n",
       "      <th>酸奶</th>\n",
       "      <th>香料</th>\n",
       "      <th>鸡蛋</th>\n",
       "      <th>黄瓜</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     冰淇淋     洋葱     牛奶     玉米    芸豆     菠萝     酸奶     香料     鸡蛋     黄瓜\n",
       "0  False   True   True  False  True  False   True   True   True  False\n",
       "1  False   True  False  False  True   True   True   True   True  False\n",
       "2  False  False   True   True  True  False   True  False  False   True\n",
       "3   True   True  False   True  True  False  False  False   True  False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将数据集转换为布尔矩阵\n",
    "te = TransactionEncoder()   #定义规则   ，不加布尔矩阵，就无法满足我数据的需求\n",
    "# te_ary = te.fit(dataset).transform(dataset) \n",
    "# 也可以写成\n",
    "te_ary = te.fit_transform(dataset) # 应用到数据\n",
    "df = pd.DataFrame(te_ary,columns=te.columns_)  #转型   关联规则建模中参数必须用到df类型的\n",
    "df    # 利用数据当列，bool值当值来填充，True代表是否在原始的数据集第一个列表中出现、第二个列表中出现、..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bfd83464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True,  True, False,  True, False,  True,  True,  True,\n",
       "        False],\n",
       "       [False,  True, False, False,  True,  True,  True,  True,  True,\n",
       "        False],\n",
       "       [False, False,  True,  True,  True, False,  True, False, False,\n",
       "         True],\n",
       "       [ True,  True, False,  True,  True, False, False, False,  True,\n",
       "        False]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "te_ary"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b233436",
   "metadata": {},
   "source": [
    "2.建模-使用apriori关联规则-得到频繁项集"
   ]
  },
  {
   "cell_type": "raw",
   "id": "69619fb5",
   "metadata": {},
   "source": [
    "关联规则：关联规则一般提供的数据是没有列名的\n",
    "        原理：发现事物之间的关联性，可以定义为某个事件发生另外的事件就会随之发生\n",
    "        怎么发现事物之间存在关联性：\n",
    "            两个事物同时发生的概率非常高，就可以定义为两个事物之间存在关联性(关联规则)\n",
    "        名词解释：\n",
    "            项集：就是项的集合，每一项就对应着一个事物\n",
    "            频繁项集：满足最小支持度的项集就是频繁项集。也就是满足关联规则\n",
    "                      频繁项集可以是多个，一个事物不是频繁一项集，那么也就一定不能构成频繁二项集、三项集....\n",
    "            支持度：表示物品同时出现的次数占整体记录数的比例\n",
    "            置信度：物品X和物品Y同时出现的次数占物品X出现次数的比例，可以计算出物品X和Y之间的关联关系\n",
    "            提升度：表示含有X的条件下同时含有Y的概率，占Y总体发生的概率。用于判断事物之间是否存在强关联规则。强关联规则认为\n",
    "                    如果X和Y存在强关联规则，X发生时Y一定会发生\n",
    "        实现算法：\n",
    "            Apriori算法:\n",
    "            FP-growth算法:\n",
    "            PrefixSpan算法:构成的频繁项集会形成一颗树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4977c6e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#min_support最小支持度，\n",
    "# use_colnames:默认False，则返回的物品组合用编号显示，为True的话直接显示物品名称\n",
    "\n",
    "model_apriori = apriori(df,min_support=0.6,use_colnames=True)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1097ea5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.00</td>\n",
       "      <td>(芸豆)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(酸奶)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(芸豆, 酸奶)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 芸豆, 鸡蛋)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   support      itemsets\n",
       "0     0.75          (洋葱)\n",
       "1     1.00          (芸豆)\n",
       "2     0.75          (酸奶)\n",
       "3     0.75          (鸡蛋)\n",
       "4     0.75      (洋葱, 芸豆)\n",
       "5     0.75      (洋葱, 鸡蛋)\n",
       "6     0.75      (芸豆, 酸奶)\n",
       "7     0.75      (芸豆, 鸡蛋)\n",
       "8     0.75  (洋葱, 芸豆, 鸡蛋)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_apriori  #itemsets表示频繁项集，只有1个名称是频繁1项集如(洋葱)，有两个名称组合的是2项集，如：(芸豆, 洋葱),有三个..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433f0d50",
   "metadata": {},
   "source": [
    "3.建模-使用fpgrowth关联规则-得到频繁项集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "82c3586a",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_fpg = fpgrowth(df=df,min_support=0.6,use_colnames=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fc54eb11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.00</td>\n",
       "      <td>(芸豆)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(酸奶)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(芸豆, 酸奶)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 芸豆, 鸡蛋)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   support      itemsets\n",
       "0     1.00          (芸豆)\n",
       "1     0.75          (鸡蛋)\n",
       "2     0.75          (酸奶)\n",
       "3     0.75          (洋葱)\n",
       "4     0.75      (芸豆, 鸡蛋)\n",
       "5     0.75      (芸豆, 酸奶)\n",
       "6     0.75      (洋葱, 鸡蛋)\n",
       "7     0.75      (洋葱, 芸豆)\n",
       "8     0.75  (洋葱, 芸豆, 鸡蛋)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_fpg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc4a362a",
   "metadata": {},
   "source": [
    "4.建模-使用fpmax关联规则-得到频繁项集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "336a4f15",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_fpm = fpmax(df=df,min_support=0.6,use_colnames=True)  #这个算法会把一项集给抛弃掉，所以下面输出没有1项集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "abc19b6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(洋葱, 芸豆, 鸡蛋)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.75</td>\n",
       "      <td>(芸豆, 酸奶)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   support      itemsets\n",
       "0     0.75  (洋葱, 芸豆, 鸡蛋)\n",
       "1     0.75      (芸豆, 酸奶)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_fpm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a454fb39",
   "metadata": {},
   "source": [
    "5.模型评分"
   ]
  },
  {
   "cell_type": "raw",
   "id": "1f20ba54",
   "metadata": {},
   "source": [
    "使用association_rules方法输出模型的各种评分(支持度、置信度、提升度等等)\n",
    "    支持度：表示物品同时出现的次数占整体记录数的比例\n",
    "    置信度：物品X和物品Y同时出现的次数占物品X出现次数的比例，可以计算出物品X和Y之间的关联关系\n",
    "    提升度：表示含有X的条件下同时含有Y的概率，占Y总体发生的概率。用于判断事物之间是否存在强关联规则。强关联规则认为\n",
    "            如果X和Y存在强关联规则，X发生时Y一定会发生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "04e35f12",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlxtend.frequent_patterns import association_rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2ca0fb85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# metric:看哪种评分，可以是置信度，也可以是提升度等等..取值有'support', 'confidence', 'lift','leverage', 'conviction' 'zhangs_metric'\n",
    "# min_threshold:置信度的最小阈值\n",
    "#metric、min_threshold两个参数都是对df进行的，就是过滤条件，都是说你要看什么\n",
    "as_rule = association_rules(df=model_apriori,metric='confidence',min_threshold=0.6)   #df要为建模中得到的频繁项集的df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d9c731da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(酸奶)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(酸奶)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   antecedents consequents  antecedent support  consequent support  support  \\\n",
       "0         (洋葱)        (芸豆)                0.75                1.00     0.75   \n",
       "1         (芸豆)        (洋葱)                1.00                0.75     0.75   \n",
       "2         (洋葱)        (鸡蛋)                0.75                0.75     0.75   \n",
       "3         (鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "4         (芸豆)        (酸奶)                1.00                0.75     0.75   \n",
       "5         (酸奶)        (芸豆)                0.75                1.00     0.75   \n",
       "6         (芸豆)        (鸡蛋)                1.00                0.75     0.75   \n",
       "7         (鸡蛋)        (芸豆)                0.75                1.00     0.75   \n",
       "8     (洋葱, 芸豆)        (鸡蛋)                0.75                0.75     0.75   \n",
       "9     (洋葱, 鸡蛋)        (芸豆)                0.75                1.00     0.75   \n",
       "10    (芸豆, 鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "11        (洋葱)    (芸豆, 鸡蛋)                0.75                0.75     0.75   \n",
       "12        (芸豆)    (洋葱, 鸡蛋)                1.00                0.75     0.75   \n",
       "13        (鸡蛋)    (洋葱, 芸豆)                0.75                0.75     0.75   \n",
       "\n",
       "    confidence      lift  leverage  conviction  zhangs_metric  \n",
       "0         1.00  1.000000    0.0000         inf            0.0  \n",
       "1         0.75  1.000000    0.0000         1.0            0.0  \n",
       "2         1.00  1.333333    0.1875         inf            1.0  \n",
       "3         1.00  1.333333    0.1875         inf            1.0  \n",
       "4         0.75  1.000000    0.0000         1.0            0.0  \n",
       "5         1.00  1.000000    0.0000         inf            0.0  \n",
       "6         0.75  1.000000    0.0000         1.0            0.0  \n",
       "7         1.00  1.000000    0.0000         inf            0.0  \n",
       "8         1.00  1.333333    0.1875         inf            1.0  \n",
       "9         1.00  1.000000    0.0000         inf            0.0  \n",
       "10        1.00  1.333333    0.1875         inf            1.0  \n",
       "11        1.00  1.333333    0.1875         inf            1.0  \n",
       "12        0.75  1.000000    0.0000         1.0            0.0  \n",
       "13        1.00  1.333333    0.1875         inf            1.0  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "as_rule"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbb7c5de",
   "metadata": {},
   "source": [
    "6.在第5点的基础上增加更多的过滤条件---加筛选条件是考点"
   ]
  },
  {
   "cell_type": "raw",
   "id": "0940b059",
   "metadata": {},
   "source": [
    "查看提升度>=1.2规则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1b79eaf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "as_lift = association_rules(df=model_apriori,metric='lift',min_threshold=1.2)   #df要为建模中得到的频繁项集的df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d10c01fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  antecedents consequents  antecedent support  consequent support  support  \\\n",
       "0        (洋葱)        (鸡蛋)                0.75                0.75     0.75   \n",
       "1        (鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "2    (洋葱, 芸豆)        (鸡蛋)                0.75                0.75     0.75   \n",
       "3    (芸豆, 鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "4        (洋葱)    (芸豆, 鸡蛋)                0.75                0.75     0.75   \n",
       "5        (鸡蛋)    (洋葱, 芸豆)                0.75                0.75     0.75   \n",
       "\n",
       "   confidence      lift  leverage  conviction  zhangs_metric  \n",
       "0         1.0  1.333333    0.1875         inf            1.0  \n",
       "1         1.0  1.333333    0.1875         inf            1.0  \n",
       "2         1.0  1.333333    0.1875         inf            1.0  \n",
       "3         1.0  1.333333    0.1875         inf            1.0  \n",
       "4         1.0  1.333333    0.1875         inf            1.0  \n",
       "5         1.0  1.333333    0.1875         inf            1.0  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "as_lift"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d53947e5",
   "metadata": {},
   "source": [
    "7.查看支持度大于0.6，置信度大于0.6，提升度大于1的规则---加筛选条件是考点"
   ]
  },
  {
   "cell_type": "raw",
   "id": "58796200",
   "metadata": {},
   "source": [
    "直接用dataframe原生的方法过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ce7c1fe8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(酸奶)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(酸奶)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(芸豆)</td>\n",
       "      <td>(洋葱, 鸡蛋)</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   antecedents consequents  antecedent support  consequent support  support  \\\n",
       "0         (洋葱)        (芸豆)                0.75                1.00     0.75   \n",
       "1         (芸豆)        (洋葱)                1.00                0.75     0.75   \n",
       "2         (洋葱)        (鸡蛋)                0.75                0.75     0.75   \n",
       "3         (鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "4         (芸豆)        (酸奶)                1.00                0.75     0.75   \n",
       "5         (酸奶)        (芸豆)                0.75                1.00     0.75   \n",
       "6         (芸豆)        (鸡蛋)                1.00                0.75     0.75   \n",
       "7         (鸡蛋)        (芸豆)                0.75                1.00     0.75   \n",
       "8     (洋葱, 芸豆)        (鸡蛋)                0.75                0.75     0.75   \n",
       "9     (洋葱, 鸡蛋)        (芸豆)                0.75                1.00     0.75   \n",
       "10    (芸豆, 鸡蛋)        (洋葱)                0.75                0.75     0.75   \n",
       "11        (洋葱)    (芸豆, 鸡蛋)                0.75                0.75     0.75   \n",
       "12        (芸豆)    (洋葱, 鸡蛋)                1.00                0.75     0.75   \n",
       "13        (鸡蛋)    (洋葱, 芸豆)                0.75                0.75     0.75   \n",
       "\n",
       "    confidence      lift  leverage  conviction  zhangs_metric  \n",
       "0         1.00  1.000000    0.0000         inf            0.0  \n",
       "1         0.75  1.000000    0.0000         1.0            0.0  \n",
       "2         1.00  1.333333    0.1875         inf            1.0  \n",
       "3         1.00  1.333333    0.1875         inf            1.0  \n",
       "4         0.75  1.000000    0.0000         1.0            0.0  \n",
       "5         1.00  1.000000    0.0000         inf            0.0  \n",
       "6         0.75  1.000000    0.0000         1.0            0.0  \n",
       "7         1.00  1.000000    0.0000         inf            0.0  \n",
       "8         1.00  1.333333    0.1875         inf            1.0  \n",
       "9         1.00  1.000000    0.0000         inf            0.0  \n",
       "10        1.00  1.333333    0.1875         inf            1.0  \n",
       "11        1.00  1.333333    0.1875         inf            1.0  \n",
       "12        0.75  1.000000    0.0000         1.0            0.0  \n",
       "13        1.00  1.333333    0.1875         inf            1.0  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "as_rule[(as_rule['support']>0.6) & (as_rule['confidence']>0.6) & (as_rule['lift']>=1)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e097e243",
   "metadata": {},
   "source": [
    "8.查看antecedents中项集只包含(芸豆, 鸡蛋)---加筛选条件是考点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "da6bee68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(洋葱)</td>\n",
       "      <td>(芸豆, 鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   antecedents consequents  antecedent support  consequent support  support  \\\n",
       "11        (洋葱)    (芸豆, 鸡蛋)                0.75                0.75     0.75   \n",
       "\n",
       "    confidence      lift  leverage  conviction  zhangs_metric  \n",
       "11         1.0  1.333333    0.1875         inf            1.0  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "as_rule[as_rule['consequents']=={'鸡蛋','芸豆'}]    #这里只能用大括号表示集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14a679d4",
   "metadata": {},
   "source": [
    "9.删除规则 \"(洋葱,芸豆)->鸡蛋\"    提供以下两种方式，但第一种方式是考试的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "41ce87d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     False\n",
       "1     False\n",
       "2     False\n",
       "3     False\n",
       "4     False\n",
       "5     False\n",
       "6     False\n",
       "7     False\n",
       "8      True\n",
       "9     False\n",
       "10    False\n",
       "11    False\n",
       "12    False\n",
       "13    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "antecedents_sale = as_rule['antecedents'] == frozenset({'洋葱','芸豆'})  #返回true和false的集合\n",
    "consequents_sale = as_rule['consequents'] == frozenset({'鸡蛋'})\n",
    "final_sale = (antecedents_sale&consequents_sale)\n",
    "final_sale \n",
    "as_rule.loc[~final_sale]  #加了波浪符号或者负号表示筛选出相反的数据，也就是选择不同时含有上述条件的选项 ，即删除以上条件的数据\n",
    "as_rule.loc[final_sale]  #不加了波浪符号或者负号表示筛选出以上条件的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "71f86ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(洋葱, 芸豆)</td>\n",
       "      <td>(鸡蛋)</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.333333</td>\n",
       "      <td>0.1875</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  antecedents consequents  antecedent support  consequent support  support  \\\n",
       "8    (洋葱, 芸豆)        (鸡蛋)                0.75                0.75     0.75   \n",
       "\n",
       "   confidence      lift  leverage  conviction  zhangs_metric  \n",
       "8         1.0  1.333333    0.1875         inf            1.0  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "as_rule[(as_rule['antecedents'] == {'洋葱','芸豆'}) & (as_rule['consequents']== {'鸡蛋'})]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
